﻿using System;
using System.Threading;
using Nutilities.Threading;
using SharedLibrary;

namespace Auktionshuset___Server
{
    public class AuctionThread : ThreadClass
    {
        private readonly Auction _auction;
        private readonly Broadcaster _broadcaster;

        public AuctionThread(Auction auction, Broadcaster broadcaster)
        {
            _auction = auction;
            _broadcaster = broadcaster;

            Name = "Auction[" + auction.Id + "]: " + auction.Title;
        }

        public override void Run()
        {
            Thread.Sleep(30000);

            int n = 1;
            while (Running)
            {
                switch (n)
                {
                    case 1:
                        _auction.StatusMessage = "Første";
                        break;
                    case 2:
                        _auction.StatusMessage = "Anden";
                        break;
                    case 3:
                        _auction.StatusMessage = "Solgt til " + _auction.HighestBidder.Alias + " for " + _auction.CurrentBid + " kr";
                        _auction.Active = false;
                        Terminate();
                        break;
                }

                Console.WriteLine(_auction.StatusMessage);

                _broadcaster.UpdateTo(Command.UPDATE_AUCTION, _auction, _auction.Subscribers);

                Thread.Sleep(10000);
                n++;
            }
        }
    }
}
